﻿
'=====================================================================
'模 块 名：mdlPrinter

'描    述：打印机操作模块

'依赖模块：

'使用方法：(1)
'          

'原始版本：1.00
'作    者：费鸣杰
'完成日期：2021-08-12

'修改版本：
'修改作者：
'修改日期：
'修改记录：
'          
'=====================================================================
Imports System.Drawing
Imports System.IO

Namespace FL_8x0.Common

    Module mdlPrinter

        '应用函数
        <Runtime.InteropServices.DllImport("winspool.drv")>
        Private Function SetDefaultPrinter(Name As String) As Boolean
        End Function


        '外部变量
        Public Const DefaultPdfPrinter As String = "Microsoft Print To PDF"



        '=====================================================================
        '函 数 名: PrintFile
        '参    数: strFilePath  = 文件路径 
        '          strPrinter   = 要使用的打印机名称(Nothing=使用默认打印机)
        '          intWaitMs    = 打印等待时间(strPrinter有效时使用)
        '返 回 值: 
        '功能描述: 打印文件
        '=====================================================================
        Public Sub PrintFile(strFilePath As String, strPrinter As String, intWaitMs As Integer)
            Dim p As Process = New Process()
            Dim strDefaultPrinter As String = GetDefaultName()

            Try
                If IsNothing(strFilePath) Then
                    Return
                End If

                '不显示调用程序窗口,但是对于某些应用无效
                p.StartInfo.CreateNoWindow = True
                p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden

                '采用操作系统自动识别的模式
                p.StartInfo.UseShellExecute = True

                '要打印的文件路径，可以是WORD,EXCEL,PDF,TXT等等
                p.StartInfo.FileName = strFilePath

                '指定执行的动作，是打印，即print，打开是 open
                p.StartInfo.Verb = "print"

                If Not IsNothing(strPrinter) Then
                    '获取当前默认打印机
                    strDefaultPrinter = GetDefaultName()
                    If (strDefaultPrinter <> strPrinter) Then
                        '将指定的打印机设为默认打印机
                        SetDefaultName(strPrinter)
                    End If
                End If
                p.EnableRaisingEvents = True
                AddHandler p.Exited, Sub(sender As Object, e As EventArgs)
                                         Call New FileInfo(strFilePath).Delete()
                                     End Sub

                ''开始打印
                p.Start()

                If Not IsNothing(strPrinter) Then
                    '等待
                    If intWaitMs > 0 Then
                        p.WaitForExit(intWaitMs)
                    End If
                    '将默认打印机还原
                    SetDefaultName(strDefaultPrinter)
                End If
            Catch ex As Exception
                log.WriteLogError("PrintFile():" & strFilePath & strPrinter, ex.Message)
            End Try
        End Sub


        '=====================================================================
        '函 数 名: ListName
        '参    数: 
        '返 回 值: 打印机名称列表
        '功能描述: 列举打印机
        '=====================================================================
        Public Function ListName() As ArrayList

            If Printing.PrinterSettings.InstalledPrinters.Count <= 0 Then
                Return Nothing
            End If

            Try
                Dim strPrinter As String
                Dim aryList As New ArrayList

                '列举所有打印机
                For Each strPrinter In Printing.PrinterSettings.InstalledPrinters
                    aryList.Add(strPrinter)
                Next strPrinter
                Return aryList
            Catch ex As Exception
                Return Nothing
            End Try
        End Function


        '=====================================================================
        '函 数 名: PrinterIsValid
        '参    数: strPrinter = 打印机名称
        '返 回 值: False   = 无效
        '          True    = 有效
        '功能描述: 打印机是否有效
        '=====================================================================
        Public Function PrinterIsValid(strPrinter As String) As Boolean
            Dim aryPrinter As ArrayList
            Dim strTmp As String

            aryPrinter = ListName()
            If Not IsNothing(aryPrinter) Then
                '列举所有打印机
                For Each strTmp In aryPrinter
                    If strTmp = strPrinter Then
                        Return True
                    End If
                Next strTmp
            End If

            Return False
        End Function


        '=====================================================================
        '函 数 名: GetDefaultName
        '参    数: 
        '返 回 值: 默认打印机名称
        '功能描述: 获取默认打印机名
        '=====================================================================
        Public Function GetDefaultName() As String
            Dim strPrinter As String

            Try
                strPrinter = Nothing

                Dim prtdoc As Printing.PrintDocument = New Printing.PrintDocument()
                strPrinter = prtdoc.PrinterSettings.PrinterName.ToString
                prtdoc.Dispose()
                If InStr(strPrinter, "未设置默认打印机") Then
                    strPrinter = Nothing
                End If

            Catch ex As Exception
                Return Nothing
            End Try

            Return strPrinter
        End Function


        '=====================================================================
        '函 数 名: SetDefaultName
        '参    数: strPrinter = 要设置的打印机名称
        '返 回 值: 默认打印机名称
        '功能描述: 设置默认打印机名
        '=====================================================================
        Public Function SetDefaultName(ByVal strPrinter As String) As String
            If strPrinter IsNot Nothing Then
                If SetDefaultPrinter(strPrinter) = True Then
                    Return strPrinter
                Else
                    Return Nothing
                End If
            Else
                Return Nothing
            End If
        End Function

    End Module
End Namespace